package com.relytosoft.mizar.media.host.emqx;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.relytosoft.dubhe.common.core.constant.CommonConstants;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.mizar.media.common.constant.MediaMqConstants;
import com.relytosoft.mizar.media.common.emqx.client.MqttOp;
import com.relytosoft.mizar.media.common.entity.pojo.dto.FaceGroupReqDto;
import com.relytosoft.mizar.media.common.entity.pojo.dto.FacePersonReqDto;
import com.relytosoft.mizar.media.external.api.analyze.service.PersonService;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * PersonMqttMessageListener
 *
 * @Author SunLi
 * @Date: 2022/5/31 10:22
 * @Version 1.0
 */
@Component
public class PersonMqttMessageListener  implements com.relytosoft.mizar.media.common.emqx.listener.PersonMqttMessageListener{

    private static final Logger log = LoggerFactory.getLogger(FaceMqttMessageListener.class);

    @Resource
    public PersonService personService;

    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        if(topic.equals(MediaMqConstants.PERSON_TOPIC)){
            boolean isConsume = false;
            String mqttMsg = new String(message.getPayload(), "utf-8");
            try {
                JSONObject jsonObject = JSON.parseObject(mqttMsg);
                String opType = jsonObject.getString("OpType");
                String data = jsonObject.getString("Data");
                switch (opType) {
                    //添加人脸库
                    case "insertGroup":
                        isConsume = createGroup(data);
                        break;
                    //删除人脸库
                    case "deleteGroup":
                        isConsume = deleteGroup(data);
                        break;
                    //添加人脸
                    case "insertPerson":
                        isConsume = createPerson(data);
                        break;
                    //删除人脸
                    case "deletePerson":
                        isConsume = deletePerson(data);
                        break;
                    default:
                        break;
                }
                log.info("线程名称：【" + Thread.currentThread().getName() + "】" + "接收的主题：" + topic + ",接收的操作类型：" + opType + "，执行结果：" + isConsume);
            }
            catch (Exception e){
                log.info("线程名称：【" + Thread.currentThread().getName() + "】" + "接收的主题：" + topic + ",处理发生错误：" + e.getMessage());
            }
        }

    }

    /**
     * 人脸库添加
     * @param data 人脸库信息
     * @return 消费结果
     */
    private boolean createGroup(String data){
        FaceGroupReqDto faceGroupReqDto=JSON.parseObject(data,FaceGroupReqDto.class);
        ResponseResult result=personService.createGroup(faceGroupReqDto);
        if(result.getCode()== CommonConstants.SUCCESS){
            //发送人脸库ID同步消息
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("OpType","updateGroupId");
            jsonObject.put("Data", result.getData());
            MqttOp.publish(MediaMqConstants.PERSON_DATA_TOPIC,JSON.toJSONString(jsonObject));
            return true;
        }
        log.info("人脸库同步添加bitman时,发生错误：" + result.getMsg());
        return false;
    }

    /**
     * 人脸库删除
     * @param data 人脸库信息
     * @return 消费结果
     */
    private boolean deleteGroup(String data){
        FaceGroupReqDto faceGroupReqDto=JSON.parseObject(data,FaceGroupReqDto.class);
        ResponseResult result=personService.deleteGroup(faceGroupReqDto);
        if(result.getCode()== CommonConstants.SUCCESS){
            return true;
        }
        log.info("人脸库同步删除bitman时,发生错误：" + result.getMsg());
        return false;
    }
    /**
     * 人脸添加
     * @param data 人脸信息
     * @return 消费结果
     */
    private boolean createPerson(String data){
        FacePersonReqDto facePersonReqDto=JSON.parseObject(data,FacePersonReqDto.class);
        ResponseResult result=personService.createPerson(facePersonReqDto);
        if(result.getCode()== CommonConstants.SUCCESS){
            //发送人脸ID同步消息
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("OpType","updatePersonId");
            jsonObject.put("Data", result.getData());
            MqttOp.publish(MediaMqConstants.PERSON_DATA_TOPIC,JSON.toJSONString(jsonObject));
            return true;
        }
        log.info("人脸同步添加bitman时,发生错误：" + result.getMsg());
        return false;
    }

    /**
     * 人脸删除
     * @param data 人脸信息
     * @return 消费结果
     */
    private boolean deletePerson(String data){
        FacePersonReqDto facePersonReqDto=JSON.parseObject(data,FacePersonReqDto.class);
        ResponseResult result=personService.deletePerson(facePersonReqDto);
        if(result.getCode()== CommonConstants.SUCCESS){
            return true;
        }
        log.info("人脸同步删除bitman时,发生错误：" + result.getMsg());
        return false;
    }
}
